package edu.wit.system.service.impl;

import cn.hutool.core.convert.Convert;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import edu.wit.common.constant.AffairsConstant;
import edu.wit.common.constant.CommonConstant;
import edu.wit.common.db.page.TableSupport;
import edu.wit.common.db.service.impl.BaseServiceImpl;
import edu.wit.common.exception.CustomException;
import edu.wit.common.exception.ResultEnums;
import edu.wit.common.security.service.TokenService;
import edu.wit.common.utils.ServletUtils;
import edu.wit.common.utils.UUIDGenUtil;
import edu.wit.common.web.PageResult;
import edu.wit.system.domain.*;
import edu.wit.system.domain.dto.AffairsApplyWorkflowDto;
import edu.wit.system.domain.dto.AffairsApplyWorkflowNodeUserDto;
import edu.wit.system.domain.dto.AffairsApplyWrokflowNodeDto;
import edu.wit.system.domain.vo.AffairsApplyWorkflowExamineVo;
import edu.wit.system.domain.vo.AffairsApplyWorkflowVo;
import edu.wit.system.domain.vo.ExamineIdsVo;
import edu.wit.system.domain.vo.PersonApplyStatusVo;
import edu.wit.system.feign.WagelFeign;
import edu.wit.system.mapper.AffairsApplyWorkflowCompanyMapper;
import edu.wit.system.mapper.AffairsApplyWorkflowMapper;
import edu.wit.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.Valid;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("affairsApplyWorkflowService")
public class AffairsApplyWorkflowServiceImpl extends BaseServiceImpl<AffairsApplyWorkflowMapper, AffairsApplyWorkflow> implements AffairsApplyWorkflowService {

	@Autowired
	private AffairsWorkflowService affairsWorkflowService;

	@Autowired
	private AffairsWorkflowNodeService affairsWorkflowNodeService;

	@Autowired
	private AffairsWorkflowNodeUserService affairsWorkflowNodeUserService;

	@Autowired
	private AffairsApplyWrokflowNodeService affairsApplyWrokflowNodeService;

	@Autowired
	private AffairsApplyWorkflowNodeUserService affairsApplyWorkflowNodeUserService;

	@Autowired
	private AffairsApplyWorkflowCompanyMapper affairsApplyWorkflowCompanyMapper;

	@Autowired
	private AffairsApplyWorkflowMapper affairsApplyWorkflowMapper;

	@Autowired
	private TokenService tokenService;

	@Autowired
	WagelFeign wageFeign;

	private SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyy-MM");
	@Override
	public PageResult<AffairsApplyWorkflowDto> getApplyList(Map<String, Object> params) {
//      暂时未开放小程序申请功能
//		if(params.get("applyUserId") == null){
//			/**
//			 *    等于null说明 是pc端发来的请求 则可以通过token获取用户名id
//			 */
//			String username = redisTemplate.opsForValue().get(ServletUtils.getRequest().getHeader("token")).toString();
//			params.put("applyUserId", baseMapper.selectUserByUsername(username).getId());
//		}
		if (params.get("corporation") != null && params.get("corporation") !="") {
			params.put("companyId",params.get("corporation"));
		}

		String companyId = (String) params.get("companyId");
		String workflowType = (String) params.get("workflowType");

		QueryWrapper<AffairsApplyWorkflow> wrapper = new QueryWrapper<>();
		wrapper.eq("workflow_type",String.valueOf(params.get("workflowType")));
		if (params.get("startTime") != null) {
			wrapper.ge("start_time",params.get("startTime"));
		}
		if (params.get("endTime") != null) {
			wrapper.le("end_time",params.get("endTime"));
		}
		if (params.get("status") != null) {
			wrapper.eq("apply_status",String.valueOf(params.get("status")));
		}
		if (params.get("applyUserId") != null) {
			wrapper.eq("apply_user_id",Long.parseLong(params.get("applyUserId") + ""));
		}

		IPage<AffairsApplyWorkflow> tPage = new Page<>(Integer.parseInt(params.get("pageNum")+""), Integer.parseInt(params.get("pageSize")+""));
		tPage = baseMapper.selectPage(tPage, wrapper);
		List<AffairsApplyWorkflow> list = tPage.getRecords();

		List<AffairsApplyWorkflowDto> dtoList = Convert.toList(AffairsApplyWorkflowDto.class, list);
		List<AffairsApplyWorkflowDto> corpResultList = new ArrayList<>();
		for(AffairsApplyWorkflowDto affairsApplyWorkflowDto : dtoList){
			affairsApplyWorkflowDto.setApplyUserName(affairsApplyWorkflowDto.getApplyUserId()+"");
			String personId = affairsApplyWorkflowCompanyMapper.selectPersonIdByApplyWorkflowId(affairsApplyWorkflowDto.getApplyId()+"");
			affairsApplyWorkflowDto.setPersonId(personId);
			if(workflowType.equals("劳务代发审批")){
				AffairsApplyWorkflowCompany affairsApplyWorkflowCompany = affairsApplyWorkflowCompanyMapper.selectOne(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("affairs_apply_workflow_id", affairsApplyWorkflowDto.getApplyId()));
				List<Long> personIds  = new ArrayList<>();
				String[] personIdsStr = affairsApplyWorkflowCompany.getPersonIds().contains("*") ? affairsApplyWorkflowCompany.getPersonIds().split("\\*") : new String[]{affairsApplyWorkflowCompany.getPersonIds()};
				for(String personId1 :personIdsStr){
					personIds.add(Long.parseLong(personId1));
				}
				affairsApplyWorkflowDto.setPersonIds(personIds);
			}
			//流程所属劳务公司名称和ID
			AffairsWorkflow affairsWorkflow = affairsWorkflowService.selectWorkflowById(affairsApplyWorkflowDto.getWorkflowId());
			affairsApplyWorkflowDto.setCompanyId(affairsWorkflow.getCorporation());
			affairsApplyWorkflowDto.setCompanyName(affairsApplyWorkflowMapper.selectCompanyNameByCompanyId(Long.parseLong(affairsWorkflow.getCorporation())));

			if (affairsApplyWorkflowDto.getCompanyId().equals(companyId) || companyId == null) {
				corpResultList.add(affairsApplyWorkflowDto);
			}

		}
		if (companyId != null && workflowType.equals("劳务代发审批")) {
			return PageResult.success(corpResultList,tPage.getTotal());
		} else {
			return PageResult.success(dtoList,tPage.getTotal());
		}
	}

	@Override
	public PageResult<AffairsApplyWorkflowDto> getExamineList(Map<String, Object> params) {
		String userIdByHeader = ServletUtils.getRequest().getHeader("user_id");
		params.put("visible", 0);
		Long userId = null;
		if(params.get("examineUserId") == null){
			/**
			 *    等于null说明 是pc端发来的请求 则可以通过token获取用户名
			 */
//			userId = Long.valueOf(baseMapper.selectUserByUsername(username).getId());
			params.put("examineUserId", userIdByHeader);
			userId = Long.parseLong(userIdByHeader);
		} else {
			userId = Long.valueOf((String) params.get("examineUserId"));
		}
		TableSupport.startPage(null);
		List<AffairsApplyWorkflowDto> result = baseMapper.getExamineList(params);
		PageInfo<AffairsApplyWorkflowDto> pageInfo = new PageInfo<>(result);
		List<AffairsApplyWorkflowDto> list = pageInfo.getList();
		if(list != null && list.size() > 0) {
			List<Long> applyIdList = new ArrayList<>();
			for(AffairsApplyWorkflowDto affairsApplyWorkflowDto : list) {
				if(affairsApplyWorkflowCompanyMapper.selectPersonIdByApplyWorkflowId(affairsApplyWorkflowDto.getApplyId()+"") != null){
					affairsApplyWorkflowDto.setPersonId(affairsApplyWorkflowCompanyMapper.selectPersonIdByApplyWorkflowId(affairsApplyWorkflowDto.getApplyId()+""));
				}
				if(affairsApplyWorkflowDto.getWorkflowType().equals("劳务代发审批")){
					AffairsApplyWorkflowCompany affairsApplyWorkflowCompany = affairsApplyWorkflowCompanyMapper.selectOne(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("affairs_apply_workflow_id", affairsApplyWorkflowDto.getApplyId()));
					List<Long> personIds  = new ArrayList<>();
					String[] personIdsStr = affairsApplyWorkflowCompany.getPersonIds().contains("*") ? affairsApplyWorkflowCompany.getPersonIds().split("\\*") : new String[]{affairsApplyWorkflowCompany.getPersonIds()};
					for(String personId1 :personIdsStr){
						personIds.add(Long.parseLong(personId1));
					}
					affairsApplyWorkflowDto.setPersonIds(personIds);
				}
				applyIdList.add(affairsApplyWorkflowDto.getApplyId());
			}
			// 查询流程节点信息
			List<AffairsApplyWrokflowNode> affairsApplyWorkflowNodeList = affairsApplyWrokflowNodeService.list(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
					.in(AffairsApplyWrokflowNode::getApplyId, applyIdList)
					.eq(AffairsApplyWrokflowNode::getWorkflowNodeIscurrent, "1"));
			// 查询流程节点人员信息
			List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>().lambda()
					.in(AffairsApplyWorkflowNodeUser::getApplyId, applyIdList));

			List<AffairsApplyWrokflowNodeDto> workflowNodeList =  Convert.toList(AffairsApplyWrokflowNodeDto.class, affairsApplyWorkflowNodeList);
			List<AffairsApplyWorkflowNodeUserDto> workflowNodeUserList =  Convert.toList(AffairsApplyWorkflowNodeUserDto.class, affairsApplyWorkflowNodeUserList);
			if(params.get("examineUserId") == null){
				for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUserDto:workflowNodeUserList){
					/**
					 *    如果是小程序端 我怎么通过id获取姓名（可能是工人id，也有可能是项目方id，在两张不同的表里面）
					 *    这里是用于 查看流程详情 如果是小程序端就暂时不考虑
					 */
					affairsApplyWorkflowNodeUserDto.setExamineUserName(baseMapper.selectUsernameById(affairsApplyWorkflowNodeUserDto.getExamineUserId()));
				}
			}


			Map<Long, List<AffairsApplyWorkflowNodeUserDto>> workflowNodeUserMap = new HashMap<>();

			if(workflowNodeUserList != null && workflowNodeUserList.size() > 0) {
				for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUserDto : workflowNodeUserList) {
					if(workflowNodeUserMap.get(affairsApplyWorkflowNodeUserDto.getApplyNodeId()) == null) {
						List<AffairsApplyWorkflowNodeUserDto> tempList = new ArrayList<>();
						tempList.add(affairsApplyWorkflowNodeUserDto);
						workflowNodeUserMap.put(affairsApplyWorkflowNodeUserDto.getApplyNodeId(), tempList);
					} else {
						workflowNodeUserMap.get(affairsApplyWorkflowNodeUserDto.getApplyNodeId()).add(affairsApplyWorkflowNodeUserDto);
					}
				}
			}

			Map<Long, AffairsApplyWrokflowNodeDto> workflowNodeMap = new HashMap<>();
			if(workflowNodeList != null && workflowNodeList.size() > 0) {
				for(AffairsApplyWrokflowNodeDto affairsApplyWrokflowNodeDto : workflowNodeList) {
					affairsApplyWrokflowNodeDto.setWorkflowNodeUserList(workflowNodeUserMap.get(affairsApplyWrokflowNodeDto.getApplyNodeId()));
					workflowNodeMap.put(affairsApplyWrokflowNodeDto.getApplyId(), affairsApplyWrokflowNodeDto);
				}
			}
			for(AffairsApplyWorkflowDto affairsApplyWorkflowDto : list) {
				affairsApplyWorkflowDto.setCurrentWorkflowNode(workflowNodeMap.get(affairsApplyWorkflowDto.getApplyId()));
				boolean canCheck = false;
				if(workflowNodeMap.get(affairsApplyWorkflowDto.getApplyId()) != null) {
					for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUser : workflowNodeMap.get(affairsApplyWorkflowDto.getApplyId()).getWorkflowNodeUserList()) {
						if(affairsApplyWorkflowNodeUser.getExamineUserId().equals(userId)) {
							canCheck = true;
						}
					}
				}
				affairsApplyWorkflowDto.setCanCheck(canCheck);
			}

		}

		return PageResult.success(list, pageInfo.getTotal());
	}

	@Override
	public AffairsApplyWorkflowDto getById(Long id) {
//		String username = redisTemplate.opsForValue().get(ServletUtils.getRequest().getHeader("token")).toString();
//		String username = tokenManager.getUserFromToken(ServletUtils.getRequest().getHeader("token"));
		String username = ServletUtils.getRequest().getHeader("username");

		AffairsApplyWorkflow model = this.getOne(new QueryWrapper<AffairsApplyWorkflow>().lambda()
				.eq(AffairsApplyWorkflow::getApplyId, id));
		if (model == null) {
			throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "ID不存在");
		}
		AffairsApplyWorkflowDto affairsApplyWorkflowDto = Convert.convert(AffairsApplyWorkflowDto.class, model);

		// 查询流程节点信息
		List<AffairsApplyWrokflowNode> affairsApplyWorkflowNodeList = affairsApplyWrokflowNodeService.list(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
				.eq(AffairsApplyWrokflowNode::getApplyId, id));
		// 查询流程节点人员信息
		List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>().lambda()
				.eq(AffairsApplyWorkflowNodeUser::getApplyId, id));

		List<AffairsApplyWrokflowNodeDto> workflowNodeList =  Convert.toList(AffairsApplyWrokflowNodeDto.class, affairsApplyWorkflowNodeList);

		List<AffairsApplyWorkflowNodeUserDto> workflowNodeUserList =  Convert.toList(AffairsApplyWorkflowNodeUserDto.class, affairsApplyWorkflowNodeUserList);
		for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUserDto:workflowNodeUserList){
			String examineUsername = baseMapper.selectUsernameById(affairsApplyWorkflowNodeUserDto.getExamineUserId());
			if(examineUsername == null){
				examineUsername = baseMapper.selectUsernameByWorkerId(affairsApplyWorkflowNodeUserDto.getExamineUserId());
			}
			affairsApplyWorkflowNodeUserDto.setExamineUserName(examineUsername);
		}
		Map<Long, List<AffairsApplyWorkflowNodeUserDto>> workflowNodeUserMap = new HashMap<>();
		if(workflowNodeUserList != null && workflowNodeUserList.size() > 0) {
			for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUserDto : workflowNodeUserList) {
				if(workflowNodeUserMap.get(affairsApplyWorkflowNodeUserDto.getApplyNodeId()) == null) {
					List<AffairsApplyWorkflowNodeUserDto> tempList = new ArrayList<>();
					tempList.add(affairsApplyWorkflowNodeUserDto);
					workflowNodeUserMap.put(affairsApplyWorkflowNodeUserDto.getApplyNodeId(), tempList);
				} else {
					workflowNodeUserMap.get(affairsApplyWorkflowNodeUserDto.getApplyNodeId()).add(affairsApplyWorkflowNodeUserDto);
				}
			}
		}
		if(workflowNodeList != null && workflowNodeList.size() > 0) {
			for(AffairsApplyWrokflowNodeDto affairsApplyWrokflowNodeDto : workflowNodeList) {
				affairsApplyWrokflowNodeDto.setWorkflowNodeUserList(workflowNodeUserMap.get(affairsApplyWrokflowNodeDto.getApplyNodeId()));

				if(affairsApplyWrokflowNodeDto.getWorkflowNodeIscurrent().equals(CommonConstant.YesNo.YES)) {
					affairsApplyWorkflowDto.setCurrentWorkflowNode(affairsApplyWrokflowNodeDto);
					boolean canCheck = false;
					if(affairsApplyWrokflowNodeDto != null) {
						for(AffairsApplyWorkflowNodeUserDto affairsApplyWorkflowNodeUser : affairsApplyWrokflowNodeDto.getWorkflowNodeUserList()) {
							if(affairsApplyWorkflowNodeUser.getExamineUserId().equals(baseMapper.selectUserByUsername(username).getId())) {
								canCheck = true;
							}
						}
					}
					affairsApplyWorkflowDto.setCanCheck(canCheck);
				}
			}
		}

		affairsApplyWorkflowDto.setWorkflowNodeList(workflowNodeList);

		String personId = affairsApplyWorkflowCompanyMapper.selectPersonIdByApplyWorkflowId(id+"");
		affairsApplyWorkflowDto.setPersonId(personId);

		return affairsApplyWorkflowDto;
	}

	@Transactional
	@Override
	@CacheEvict(value = "bank:wage", allEntries = true)
	public void apply(AffairsApplyWorkflowVo vo)
	{
		// 查询流程信息
		AffairsWorkflow affairsWorkflow = affairsWorkflowService.getOne(new QueryWrapper<AffairsWorkflow>().lambda()
				.eq(AffairsWorkflow::getWorkflowId, vo.getWorkflowId()));

		String username = tokenService.getLoginUserByToken().getUsername();
		User user = baseMapper.selectUserByUsername(username);
		String projectAdminId = user.getPid().equals("0") ? user.getId() : user.getPid();
		Long userId = Long.valueOf(user.getId());

		List<ExamineIdsVo> examineIdsVO =vo.getExamineIds();
		examineIdsVO.forEach(examineIdVo -> {
			Long applyId = UUIDGenUtil.generate();
			AffairsApplyWorkflow affairsApplyWorkflow = new AffairsApplyWorkflow();
			org.springframework.beans.BeanUtils.copyProperties(affairsWorkflow, affairsApplyWorkflow);
			affairsApplyWorkflow.setApplyId(applyId);
			affairsApplyWorkflow.setApplyUserId(userId);
			affairsApplyWorkflow.setApplyTime(new Date());
			affairsApplyWorkflow.setApplyReason(vo.getApplyReason());
			affairsApplyWorkflow.setStartTime(new Date());
			affairsApplyWorkflow.setApplyStatus(AffairsConstant.ApplyStatus.WAITING);
			AffairsApplyWorkflowCompany affairsApplyWorkflowCompany=new AffairsApplyWorkflowCompany();
			if(examineIdVo.getFine() != null){
				/**
				 *  Fine是月工资独有的属性，所以不为null时就是 申请月工资审批流程                                   更新 流程-劳务公司关联表
				 */
				affairsApplyWorkflow.setAttachInfo(new SimpleDateFormat("yyyy-MM").format(examineIdVo.getAttendanceTime())+"*"+examineIdVo.getPersonName()+"*"+examineIdVo.getAttendanceDays()+"*"+examineIdVo.getReferenceMonthlyWage()+"*"+examineIdVo.getActualMonthlyWage()+"*"+examineIdVo.getBonus()+"*"+examineIdVo.getFine()+"*"+vo.getProjectId());
				affairsApplyWorkflowCompany.setAffairsApplyWorkflowId(applyId+"");
				affairsApplyWorkflowCompany.setApplyWageMonth(examineIdVo.getAttendanceTime());
				affairsApplyWorkflowCompany.setCompanyId(vo.getCompanyId());
				affairsApplyWorkflowCompany.setCompanyName(baseMapper.selectCompanyNameByCompanyId(vo.getCompanyId()));
				affairsApplyWorkflowCompany.setPersonId(examineIdVo.getWorkerId());
				affairsApplyWorkflowCompany.setActualMonthlyWage(examineIdVo.getActualMonthlyWage());
				affairsApplyWorkflowCompanyMapper.insert(affairsApplyWorkflowCompany);

			}else if(examineIdVo.getServiceCorporationName() !=null){
				/**
				 *  ServiceCorporationName是劳务代发独有的属性，所以不为null时就是 劳务代发审批流程                  更新 流程-劳务公司关联表
				 */
				affairsApplyWorkflow.setAttachInfo(new SimpleDateFormat("yyyy-MM").format(examineIdVo.getAttendanceTime())+"*"+examineIdVo.getServiceCorporationName()+"*"+examineIdVo.getAttendanceNum()+"*"+examineIdVo.getActualMonthlyWage()+"*"+examineIdVo.getShouldIssuedMonthlyWage()+"*"+examineIdVo.getIssuedMonthlyWage()+"*"+examineIdVo.getUnissuedMonthlyWage()+"*"+vo.getProjectId());
				affairsApplyWorkflowCompany.setAffairsApplyWorkflowId(applyId+"");
				affairsApplyWorkflowCompany.setApplyWageMonth(examineIdVo.getAttendanceTime());
				affairsApplyWorkflowCompany.setCompanyId(vo.getCompanyId());
				affairsApplyWorkflowCompany.setCompanyName(baseMapper.selectCompanyNameByCompanyId(vo.getCompanyId()));
				affairsApplyWorkflowCompany.setProjectAdminId(projectAdminId);
				String personIds = null;
				if(vo.getPersonIds() != null){
					for (Long personId : vo.getPersonIds()){
						if(personIds != null){
							personIds = personIds + "*" + personId;
						}else {
							personIds = personId + "";
						}
					}
					affairsApplyWorkflowCompany.setPersonIds(personIds);
					/**
					 *  远程调用学长接口修改人员状态~~~~
					 */
					wageFeign.commitUpdate(vo.getPersonIds(),simpleDateFormat.format(vo.getExamineIds().get(0).getAttendanceTime()),Integer.parseInt(AffairsConstant.ApplyStatus.WAITING));
				}
				affairsApplyWorkflowCompanyMapper.insert(affairsApplyWorkflowCompany);
			}
			if(vo.getAttachList() != null && vo.getAttachList().size() > 0) {
				affairsApplyWorkflow.setAttachInfo(JSON.toJSONString(vo.getAttachList()));
			}
			baseMapper.insert(affairsApplyWorkflow);

			// 查询流程节点人员信息
			List<AffairsWorkflowNodeUser> affairsWorkflowNodeUserList = affairsWorkflowNodeUserService.list(new QueryWrapper<AffairsWorkflowNodeUser>().lambda()
					.eq(AffairsWorkflowNodeUser::getWorkflowId, vo.getWorkflowId()));

			/**
			 *  利用流程节点id------存储affairs_workflow_node_user对象
			 */
			Map<Long, List<AffairsWorkflowNodeUser>> affairsWorkflowNodeUserMap = new HashMap<Long, List<AffairsWorkflowNodeUser>>();
			if(affairsWorkflowNodeUserList != null && affairsWorkflowNodeUserList.size() > 0) {
				for(AffairsWorkflowNodeUser affairsWorkflowNodeUser : affairsWorkflowNodeUserList) {
					if(affairsWorkflowNodeUserMap.get(affairsWorkflowNodeUser.getWorkflowNodeId()) == null) {
						List<AffairsWorkflowNodeUser> tempList = new ArrayList<AffairsWorkflowNodeUser>();
						tempList.add(affairsWorkflowNodeUser);
						affairsWorkflowNodeUserMap.put(affairsWorkflowNodeUser.getWorkflowNodeId(), tempList);
					} else {
						affairsWorkflowNodeUserMap.get(affairsWorkflowNodeUser.getWorkflowNodeId()).add(affairsWorkflowNodeUser);
					}
				}
			}


			// 查询流程节点信息---affairs_workflow_node对象
			List<AffairsWorkflowNode> affairsWorkflowNodeList = affairsWorkflowNodeService.list(new QueryWrapper<AffairsWorkflowNode>().lambda()
					.eq(AffairsWorkflowNode::getWorkflowId, vo.getWorkflowId()));

			List<AffairsApplyWrokflowNode> affairsApplyWorkflowNodeList = new ArrayList<AffairsApplyWrokflowNode>();
			List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = new ArrayList<AffairsApplyWorkflowNodeUser>();
			// 申请节点信息-----affairs_apply_workflow_node对象
			if(affairsWorkflowNodeList != null && affairsWorkflowNodeList.size() > 0) {
				int i=0;
				for(AffairsWorkflowNode affairsWorkflowNode : affairsWorkflowNodeList) {
					i++;
					Long applyNodeId = UUIDGenUtil.generate();

					AffairsApplyWrokflowNode affairsApplyWrokflowNode = new AffairsApplyWrokflowNode();
					org.springframework.beans.BeanUtils.copyProperties(affairsWorkflowNode, affairsApplyWrokflowNode);
					affairsApplyWrokflowNode.setApplyNodeId(applyNodeId);
					affairsApplyWrokflowNode.setApplyId(applyId);
					affairsApplyWrokflowNode.setWorkflowNodeStatus(AffairsConstant.NodeStatus.WAITING);
					if(i==1) {
						affairsApplyWrokflowNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
					} else {
						affairsApplyWrokflowNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
					}
					affairsApplyWorkflowNodeList.add(affairsApplyWrokflowNode);
					/**
					 * 通过申请节点id-----获取到刚刚在map里面存储的affairs_apply_workflow_node_user对象
					 */
					List<AffairsWorkflowNodeUser> tempList = affairsWorkflowNodeUserMap.get(affairsWorkflowNode.getWorkflowNodeId());
					if(tempList != null && tempList.size() > 0) {
						for(AffairsWorkflowNodeUser affairsWorkflowNodeUser : tempList) {
							AffairsApplyWorkflowNodeUser affairsApplyWorkflowNodeUser = new AffairsApplyWorkflowNodeUser();
							affairsApplyWorkflowNodeUser.setId(UUIDGenUtil.generate());
							affairsApplyWorkflowNodeUser.setApplyId(applyId);
							affairsApplyWorkflowNodeUser.setApplyNodeId(applyNodeId);
							/**
							 *   现在定义流程的时候，默认第一个节点是班组长节点，第二个节点是工人节点
							 */
							if(i==1){
								affairsApplyWorkflowNodeUser.setExamineUserId(affairsWorkflowNodeUser.getExamineUserId() ==0 ? examineIdVo.getWorkerLeaderId() : affairsWorkflowNodeUser.getExamineUserId() );
							}else {
								affairsApplyWorkflowNodeUser.setExamineUserId(affairsWorkflowNodeUser.getExamineUserId() ==0 ? examineIdVo.getWorkerId() : affairsWorkflowNodeUser.getExamineUserId() );
							}
							affairsApplyWorkflowNodeUserList.add(affairsApplyWorkflowNodeUser);
						}
					}
				}

				// 保存申请节点信息
				affairsApplyWrokflowNodeService.saveBatch(affairsApplyWorkflowNodeList);
				// 保存申请节点审核人员信息
				if(affairsApplyWorkflowNodeUserList != null && affairsApplyWorkflowNodeUserList.size() > 0) {
					affairsApplyWorkflowNodeUserService.saveBatch(affairsApplyWorkflowNodeUserList);
				}
			}
		});

	}

	/**
	 * 撤销
	 */
	@Override
	public void cancel(Long applyId) {
		baseMapper.updateApplyStatus(applyId,AffairsConstant.ApplyStatus.BACK);
	}

	/**
	 *  审核--------------这里是指pc端的审核 可以自动获取用户名
	 */
	@CacheEvict(value = "bank:wage", allEntries = true)
	@Override
	public void examine(@Valid AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo) {

		AffairsApplyWrokflowNode affairsApplyWrokflowNode = affairsApplyWrokflowNodeService.getById(affairsApplyWorkflowExamineVo.getApplyNodeId());

		AffairsApplyWorkflowCompany affairsApplyWorkflowCompany = affairsApplyWorkflowCompanyMapper.selectOne(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("affairs_apply_workflow_id", affairsApplyWrokflowNode.getApplyId()));
		Boolean isServiceAgency = affairsApplyWorkflowCompany.getPersonIds() == null ? false : true;
		String username = tokenService.getLoginUserByToken().getUsername();
		Long userId = Long.valueOf(baseMapper.selectUserByUsername(username).getId());

		if(affairsApplyWrokflowNode.getWorkflowNodeIscurrent().equals(CommonConstant.YesNo.YES)) {
			// 查询节点审核人员，判断是否有审核权限
			List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>()
					.lambda().eq(AffairsApplyWorkflowNodeUser::getApplyNodeId, affairsApplyWorkflowExamineVo.getApplyNodeId()));
			if(affairsApplyWorkflowNodeUserList != null && affairsApplyWorkflowNodeUserList.size() > 0) {
				boolean isCheck = false;
				for(AffairsApplyWorkflowNodeUser affairsApplyWorkflowNodeUser : affairsApplyWorkflowNodeUserList) {
					if (affairsApplyWorkflowNodeUser.getExamineUserId().equals(userId)) {
						isCheck = true;
						break;
					}
				}
				if(!isCheck) {
					throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
				}
			} else {
				throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
			}
			if("1".equals(affairsApplyWorkflowExamineVo.getType())) { // 同意
//				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
//				affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().eq("apply_node_id", affairsApplyWorkflowExamineVo.getApplyNodeId()));
				affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
				affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
				affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.PASS);
				affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
				affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
				affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

				if("1".equals(affairsApplyWrokflowNode.getWorkflowNodeIslast())) { // 最后一个节点
					baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.PASS);
					AffairsApplyWorkflow affairsApplyWorkflow = new AffairsApplyWorkflow();
					affairsApplyWorkflow.setApplyId(affairsApplyWrokflowNode.getApplyId());
					affairsApplyWorkflow.setEndTime(new Date());
					baseMapper.updateById(affairsApplyWorkflow);
					if(isServiceAgency){
						List<Long> personIds  = new ArrayList<>();
						String[] personIdsStr = affairsApplyWorkflowCompany.getPersonIds().contains("*") ? affairsApplyWorkflowCompany.getPersonIds().split("\\*") : new String[]{affairsApplyWorkflowCompany.getPersonIds()};
						for(String personId :personIdsStr){
							personIds.add(Long.parseLong(personId));
						}
						String date = simpleDateFormat.format(affairsApplyWorkflowCompany.getApplyWageMonth());

						/**
						 * ToDo
						 * 远程调用学长接口修改人员状态~~~~
						 */
						System.out.println("------------personIds"+personIds);
						wageFeign.commitUpdate(personIds,date,Integer.parseInt(AffairsConstant.ApplyStatus.PASS));
					}
				} else {
					// 将下一个节点跟新成当前节点
					AffairsApplyWrokflowNode nextNode = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
							.eq(AffairsApplyWrokflowNode::getApplyId, affairsApplyWrokflowNode.getApplyId())
							.eq(AffairsApplyWrokflowNode::getWorkflowNodeIndex, affairsApplyWrokflowNode.getWorkflowNodeIndex() + 1));
					nextNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
					affairsApplyWrokflowNodeService.updateById(nextNode);
					baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.EXAMINING);
				}

			} else if("2".equals(affairsApplyWorkflowExamineVo.getType())) {// 拒绝
//				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
//				affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().eq("apply_node_id", affairsApplyWorkflowExamineVo.getApplyNodeId()));
				affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
				affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
				affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.REFUSE);
				affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
				affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
				affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);
				if(isServiceAgency){
					List<Long> personIds  = new ArrayList<>();
					String[] personIdsStr = affairsApplyWorkflowCompany.getPersonIds().contains("*") ? affairsApplyWorkflowCompany.getPersonIds().split("\\*") : new String[]{affairsApplyWorkflowCompany.getPersonIds()};
					for(String personId :personIdsStr){
						personIds.add(Long.parseLong(personId));
					}
					/**
					 * ToDo
					 * 远程调用学长接口修改人员状态~~~~
					 */
					String date = simpleDateFormat.format(affairsApplyWorkflowCompany.getApplyWageMonth());
					wageFeign.commitUpdate(personIds,date,Integer.parseInt(AffairsConstant.ApplyStatus.REFUSE));
				}

				/**
				 * 当前节点拒绝，应走上一个节点，即将上一个节点改成当前节点1,当前节点改成0,并且将申请流程改成审核中（需要判断当前节点是否是第一个节点todo）
				 */
//				AffairsApplyWrokflowNode preNode = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
//						.eq(AffairsApplyWrokflowNode::getApplyId, affairsApplyWrokflowNode.getApplyId())
//						.eq(AffairsApplyWrokflowNode::getWorkflowNodeIndex, affairsApplyWrokflowNode.getWorkflowNodeIndex() - 1));
//				preNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
//				preNode.setExamineReason(affairsApplyWorkflowNodeEntity.getExamineReason());
//				affairsApplyWrokflowNodeService.updateById(preNode);
//				baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.EXAMINING);

				baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.REFUSE);
			}
		}
		else {
			throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "非当前节点，不能审核");
		}
	}


	/**
	 *  工人审核--------------这里是指移动端的审核 不可以自动获取用户名
	 */
//	@CacheEvict(value = "bank:wage", allEntries = true)
	@Override
	public void workerExamine(@Valid AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo, String workerId) {

		AffairsApplyWrokflowNode affairsApplyWrokflowNode = affairsApplyWrokflowNodeService.getById(affairsApplyWorkflowExamineVo.getApplyNodeId());
//		AffairsApplyWorkflowCompany affairsApplyWorkflowCompany = affairsApplyWorkflowCompanyMapper.selectOne(new QueryWrapper<AffairsApplyWorkflowCompany>().eq("affairs_apply_workflow_id", affairsApplyWrokflowNode.getApplyId()));
//		boolean isServiceAgency = affairsApplyWorkflowCompany.getPersonId() != null;

		Long userId = Long.parseLong(workerId);
		if(affairsApplyWrokflowNode.getWorkflowNodeIscurrent().equals(CommonConstant.YesNo.YES)) {
			// 查询节点审核人员，判断是否有审核权限
			List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>()
					.lambda().eq(AffairsApplyWorkflowNodeUser::getApplyNodeId, affairsApplyWorkflowExamineVo.getApplyNodeId()));
			if(affairsApplyWorkflowNodeUserList != null && affairsApplyWorkflowNodeUserList.size() > 0) {
				boolean isCheck = false;
				for(AffairsApplyWorkflowNodeUser affairsApplyWorkflowNodeUser : affairsApplyWorkflowNodeUserList) {
					if (affairsApplyWorkflowNodeUser.getExamineUserId().equals(userId)) {
						isCheck = true;
						break;
					}
				}
				if(!isCheck) {
					throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
				}
			} else {
				throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
			}
			/**
			 * 修改当前节点状态
			 */
			AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().eq("apply_node_id", affairsApplyWorkflowExamineVo.getApplyNodeId()));
			affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
			affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
			affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.WAITING);
			affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
			affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
			affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

			/**
			 * 当前节点拒绝，应走上一个节点，即将上一个节点改成当前节点1,当前节点改成0,并且将申请流程改成审核中（需要判断当前节点是否是第一个节点todo）
			 */
			AffairsApplyWrokflowNode preNode = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
					.eq(AffairsApplyWrokflowNode::getApplyId, affairsApplyWrokflowNode.getApplyId())
					.eq(AffairsApplyWrokflowNode::getWorkflowNodeIndex, affairsApplyWrokflowNode.getWorkflowNodeIndex() - 1));
			preNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
			String examineReason = workerId + affairsApplyWorkflowNodeEntity.getExamineReason() + "有异议";
			preNode.setExamineReason(examineReason);
			affairsApplyWrokflowNodeService.updateById(preNode);
			baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.EXAMINING);
		} else {
			throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "非当前节点，不能审核");
		}
	}

	/**
	 *  多个审核--------------这里是指pc端的多个审核
	 */
	@Override
	public void multipleExamine(List<AffairsApplyWorkflowExamineVo> affairsApplyWorkflowExamineVos) {
//		String username = tokenManager.getUserFromToken(ServletUtils.getRequest().getHeader("token"));
		String username = tokenService.getLoginUserByToken().getUsername();
		Long userId = Long.valueOf(baseMapper.selectUserByUsername(username).getId());
		multipleExamine(userId,affairsApplyWorkflowExamineVos);
	}
	/**
	 *  多个审核--------------这里是指小程序端的多个审核(因为小程序不走security，所以需要传入审查人id)
	 */
	@Override
	public void multipleExamine(List<AffairsApplyWorkflowExamineVo> affairsApplyWorkflowExamineVos, String examineId) {
		Long userId = Long.valueOf(examineId);
		multipleExamine(userId,affairsApplyWorkflowExamineVos);
	}
	/**
	 *  多个审核--------------封装
	 */
	public void multipleExamine(Long userId , List<AffairsApplyWorkflowExamineVo> affairsApplyWorkflowExamineVos){
		for (AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo : affairsApplyWorkflowExamineVos){
			AffairsApplyWrokflowNode affairsApplyWrokflowNode = affairsApplyWrokflowNodeService.getById(affairsApplyWorkflowExamineVo.getApplyNodeId());
			if(affairsApplyWrokflowNode.getWorkflowNodeIscurrent().equals(CommonConstant.YesNo.YES)) {
				// 查询节点审核人员，判断是否有审核权限
				List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>()
						.lambda().eq(AffairsApplyWorkflowNodeUser::getApplyNodeId, affairsApplyWorkflowExamineVo.getApplyNodeId()));
				if(affairsApplyWorkflowNodeUserList != null && affairsApplyWorkflowNodeUserList.size() > 0) {
					boolean isCheck = false;
					for(AffairsApplyWorkflowNodeUser affairsApplyWorkflowNodeUser : affairsApplyWorkflowNodeUserList) {
						if(affairsApplyWorkflowNodeUser.getExamineUserId().equals(userId)) {
							isCheck = true;
						}
					}
					if(!isCheck) {
						throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
					}
				} else {
					throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
				}
				if("1".equals(affairsApplyWorkflowExamineVo.getType())) { // 同意
					AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
					affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
					affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
					affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
					affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.PASS);
					affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
					affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
					affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

					if("1".equals(affairsApplyWrokflowNode.getWorkflowNodeIslast())) { // 最后一个节点
						baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.PASS);
						AffairsApplyWorkflow affairsApplyWorkflow = new AffairsApplyWorkflow();
						affairsApplyWorkflow.setApplyId(affairsApplyWrokflowNode.getApplyId());
						affairsApplyWorkflow.setEndTime(new Date());
						baseMapper.updateById(affairsApplyWorkflow);
					} else {
						// 将下一个节点跟新成当前节点
						AffairsApplyWrokflowNode nextNode = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
								.eq(AffairsApplyWrokflowNode::getApplyId, affairsApplyWrokflowNode.getApplyId())
								.eq(AffairsApplyWrokflowNode::getWorkflowNodeIndex, affairsApplyWrokflowNode.getWorkflowNodeIndex() + 1));
						nextNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
						affairsApplyWrokflowNodeService.updateById(nextNode);
						baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.EXAMINING);
					}

				} else if("2".equals(affairsApplyWorkflowExamineVo.getType())) {// 拒绝
					AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
					affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
					affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
					affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
					affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.REFUSE);
					affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
					affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
					affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

					baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.REFUSE);
				}
			}
			else {
				throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "非当前节点，不能审核");
			}
		}
	}

	/**
	 *  审核--------------这里是指小程序端的审核 用户名id需要前端传来
	 */
	@Override
	public void examine(AffairsApplyWorkflowExamineVo affairsApplyWorkflowExamineVo,String examineId) {
		AffairsApplyWrokflowNode affairsApplyWrokflowNode = affairsApplyWrokflowNodeService.getById(affairsApplyWorkflowExamineVo.getApplyNodeId());
		Long userId = Long.valueOf(examineId);

		if(affairsApplyWrokflowNode.getWorkflowNodeIscurrent().equals(CommonConstant.YesNo.YES)) {
			// 查询节点审核人员，判断是否有审核权限
			List<AffairsApplyWorkflowNodeUser> affairsApplyWorkflowNodeUserList = affairsApplyWorkflowNodeUserService.list(new QueryWrapper<AffairsApplyWorkflowNodeUser>()
					.lambda().eq(AffairsApplyWorkflowNodeUser::getApplyNodeId, affairsApplyWorkflowExamineVo.getApplyNodeId()));
			if(affairsApplyWorkflowNodeUserList != null && affairsApplyWorkflowNodeUserList.size() > 0) {
				boolean isCheck = false;
				for(AffairsApplyWorkflowNodeUser affairsApplyWorkflowNodeUser : affairsApplyWorkflowNodeUserList) {
					if(affairsApplyWorkflowNodeUser.getExamineUserId().equals(userId)) {
						isCheck = true;
					}
				}
				if(!isCheck) {
					throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
				}
			} else {
				throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "您没有申请权限");
			}
			if("1".equals(affairsApplyWorkflowExamineVo.getType())) { // 同意
				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
				affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
				affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
				affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
				affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.PASS);
				affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
				affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
				affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

				if("1".equals(affairsApplyWrokflowNode.getWorkflowNodeIslast())) { // 最后一个节点
					baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.PASS);
					AffairsApplyWorkflow affairsApplyWorkflow = new AffairsApplyWorkflow();
					affairsApplyWorkflow.setApplyId(affairsApplyWrokflowNode.getApplyId());
					affairsApplyWorkflow.setEndTime(new Date());
					baseMapper.updateById(affairsApplyWorkflow);
				} else {
					// 将下一个节点跟新成当前节点
					AffairsApplyWrokflowNode nextNode = affairsApplyWrokflowNodeService.getOne(new QueryWrapper<AffairsApplyWrokflowNode>().lambda()
							.eq(AffairsApplyWrokflowNode::getApplyId, affairsApplyWrokflowNode.getApplyId())
							.eq(AffairsApplyWrokflowNode::getWorkflowNodeIndex, affairsApplyWrokflowNode.getWorkflowNodeIndex() + 1));
					nextNode.setWorkflowNodeIscurrent(CommonConstant.YesNo.YES);
					affairsApplyWrokflowNodeService.updateById(nextNode);
					baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.EXAMINING);
				}

			} else if("2".equals(affairsApplyWorkflowExamineVo.getType())) {// 拒绝
				AffairsApplyWrokflowNode affairsApplyWorkflowNodeEntity = new AffairsApplyWrokflowNode();
				affairsApplyWorkflowNodeEntity.setApplyNodeId(affairsApplyWorkflowExamineVo.getApplyNodeId());
				affairsApplyWorkflowNodeEntity.setExamineUserId(userId);
				affairsApplyWorkflowNodeEntity.setExamineTime(new Date());
				affairsApplyWorkflowNodeEntity.setWorkflowNodeStatus(AffairsConstant.NodeStatus.REFUSE);
				affairsApplyWorkflowNodeEntity.setWorkflowNodeIscurrent(CommonConstant.YesNo.NO);
				affairsApplyWorkflowNodeEntity.setExamineReason(affairsApplyWorkflowExamineVo.getExamineReason());
				affairsApplyWrokflowNodeService.updateById(affairsApplyWorkflowNodeEntity);

				baseMapper.updateApplyStatus(affairsApplyWrokflowNode.getApplyId(),AffairsConstant.ApplyStatus.REFUSE);
			}
		} else {
			throw new CustomException(ResultEnums.PARAM_ERROR.getCode(), "非当前节点，不能审核");
		}
	}
	@Override
	public List<PersonApplyStatusVo> selectObjectionListByPersonId(String personId,String applyStatus) {
		Long teamId = affairsApplyWorkflowMapper.selectTeamIdByPersonId(Long.parseLong(personId));
		return affairsApplyWorkflowMapper.selectObjectionListByTeamId(teamId,applyStatus);
	}
}
